package com.welike.shibo.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.welike.shibo.eneity.Comments;
import com.welike.shibo.eneity.User;
import com.welike.shibo.mapper.CommentsMapper;
import com.welike.shibo.mapper.UserMapper;
import com.welike.shibo.mapper.VideoMapper;
import com.welike.shibo.mapper.ZanMapper;
import com.welike.shibo.service.CommentsService;
import com.welike.shibo.utils.Tools;
import com.welike.shibo.vo.page.CommentMsgPageVo;
import com.welike.shibo.vo.page.CommentPageVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author fangyu
 * @version v1.0.0
 * @since 2019-08-20 00:40
 */
@Slf4j
@Service(value = "commentsServiceImpl")
public class CommentsServiceImpl extends ServiceImpl<CommentsMapper, Comments> implements CommentsService {

    @Autowired
    private CommentsMapper commentsMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private ZanMapper zanMapper;

    @Autowired
    private VideoMapper videoMapper;

    // TODO 加工评论
    @Override
    public IPage<Comments> selectCommentPage(int page, int pageSize, String vid) {
        try {
            Page<Comments> p = new Page<>(page, pageSize);
            commentsMapper.selectCommentPage(p, vid);
            return p;
        } catch (RuntimeException e) {
            throw new RuntimeException(e.getMessage());
        }
    }


    @Override
    public Comments selectCommentTop(String vid) {
        return commentsMapper.selectCommentTop(vid);
    }

    @Override
    public Integer queryLikeCountsById(Integer cid) {
        return commentsMapper.queryLikeCountsById(cid);
    }

    @Override
    public Integer countByComment() {
        return commentsMapper.countByComment();
    }

    @Override
    public Integer countByVid(String vid) {
        return commentsMapper.countByVideoId(vid);
    }

    /**
     * 查询评论列表
     * @param page 页码
     * @param pageSize 分页大小
     * @param vid 当前视频id
     * @param uid 当前登录用户的uid
     * @return List(CommentPageVo)
     */
    @Override
    public List<CommentPageVo> getCommentPageVoList(int page, int pageSize, String vid, String uid) {
        // 通过视频查找作者信息
        String authorId = videoMapper.queryUserIdById(vid);
        User author = userMapper.queryUserByUid(authorId);

        log.info("uid ==> {}", uid);
        List<CommentPageVo> commentPageVoList = new ArrayList<>();
        Page<Comments> p = new Page<>(page, pageSize);
        IPage<Comments> commentVOList = commentsMapper.selectCommentPage(p, vid);
        for (Comments comment: commentVOList.getRecords()){
            CommentPageVo commentPageVo = new CommentPageVo();
            // 查询评论人的信息
            User commentUser = userMapper.queryUserByUid(comment.getUserId());
            commentPageVo.setId(comment.getId());
            commentPageVo.setCommentUid(comment.getUserId());
            commentPageVo.setNickName(commentUser.getNickname());
            commentPageVo.setAvatar(commentUser.getAvatar());
            commentPageVo.setContent(comment.getContent());
            commentPageVo.setCommentTime(comment.getCreateTime());
            commentPageVo.setAtType(comment.getAtType());

            // 设置评论at内容
            CommentPageVo.AtContent atContent = new CommentPageVo.AtContent();
            atContent.setAtUid(comment.getAtUserId());
            atContent.setAtNickName(userMapper.queryNickNameByUid(comment.getAtUserId()));
            commentPageVo.setAtContent(atContent);

            // 设置回复人的信息
            CommentPageVo.AuthorReply authorReply = null;
            // 通过内容判断是否已经回复
            if (Tools.isNull(comment.getReplyContent())) {
                commentPageVo.setReplyState(false);
            } else {
                commentPageVo.setReplyState(true);
                // 不为空则new 一个回复对象
                authorReply = new CommentPageVo.AuthorReply();
                authorReply.setAuthorNickName(author.getNickname());
                authorReply.setAuthorAvatar(author.getAvatar());
                authorReply.setReplyContent(comment.getReplyContent());
                authorReply.setReplyTime(comment.getReplyTime());
            }
            commentPageVo.setAuthorReply(authorReply);

            // 判断是否登录, uid为空则未登录, 未登录默认点赞状态则为false
            if (uid != null){
                // 查询点赞状态
                String result = zanMapper.queryIdByUserIdAndZanObjId(uid, String.valueOf(comment.getId()));
                if (result != null){
                    commentPageVo.setLikeStatus(true);
                }
            }

            // 最后一步
            log.info("commentPageVo ==> {}", commentPageVo);
            commentPageVoList.add(commentPageVo);
        }
        return commentPageVoList;
    }

    @Override
    public List<CommentPageVo> getCommentPageVoList(String vid, String uid) {
        // 通过视频查找作者信息
        String authorId = videoMapper.queryUserIdById(vid);
        User author = userMapper.queryUserByUid(authorId);

        log.info("uid ==> {}", uid);
        List<CommentPageVo> commentPageVoList = new ArrayList<>();
        List<Comments> commentVOList = commentsMapper.selectAllComment(vid);

        for (Comments comment: commentVOList){
            CommentPageVo commentPageVo = new CommentPageVo();
            // 查询评论人的信息
            User commentUser = userMapper.queryUserByUid(comment.getUserId());
            commentPageVo.setId(comment.getId());
            commentPageVo.setCommentUid(comment.getUserId());
            commentPageVo.setNickName(commentUser.getNickname());
            commentPageVo.setAvatar(commentUser.getAvatar());
            commentPageVo.setContent(comment.getContent());
            commentPageVo.setCommentTime(comment.getCreateTime());
            commentPageVo.setAtType(comment.getAtType());

            // 设置评论at内容
            CommentPageVo.AtContent atContent = new CommentPageVo.AtContent();
            atContent.setAtUid(comment.getAtUserId());
            atContent.setAtNickName(userMapper.queryNickNameByUid(comment.getAtUserId()));
            commentPageVo.setAtContent(atContent);

            // 设置回复人的信息
            CommentPageVo.AuthorReply authorReply = null;
            // 通过内容判断是否已经回复
            if (Tools.isNull(comment.getReplyContent())) {
                commentPageVo.setReplyState(false);
            } else {
                commentPageVo.setReplyState(true);
                // 不为空则new 一个回复对象
                authorReply = new CommentPageVo.AuthorReply();
                authorReply.setAuthorNickName(author.getNickname());
                authorReply.setAuthorAvatar(author.getAvatar());
                authorReply.setReplyContent(comment.getReplyContent());
                authorReply.setReplyTime(comment.getReplyTime());
            }
            commentPageVo.setAuthorReply(authorReply);

            // 判断是否登录, uid为空则未登录, 未登录默认点赞状态则为false
            if (uid != null){
                // 查询点赞状态
                String result = zanMapper.queryIdByUserIdAndZanObjId(uid, String.valueOf(comment.getId()));
                if (result != null){
                    commentPageVo.setLikeStatus(true);
                }
            }

            // 最后一步
            log.info("commentPageVo ==> {}", commentPageVo);
            commentPageVoList.add(commentPageVo);
        }
        return commentPageVoList;
    }

    @Override
    public List<CommentMsgPageVo> getUserCommentMsgPageList(int page, int pageSize, String uid) {
        Page<Comments> p = new Page<>(page, pageSize);
        IPage<Comments> result = commentsMapper.queryAllByAuthorIdOrderByCreateTime(p, uid);
        List<CommentMsgPageVo> commentPageVoArrayList = new ArrayList<>();
        // 单独查作者信息
        String authorNickName = userMapper.queryNickNameByUid(uid);
        String authorAvatar = userMapper.queryAvatarByUid(uid);
        // 单评论内容
        for (Comments comments: result.getRecords()){
            CommentMsgPageVo commentPageVo = new CommentMsgPageVo();
            commentPageVo.setId(comments.getId());
            commentPageVo.setCommentUid(comments.getUserId());
            commentPageVo.setContent(comments.getContent());
            commentPageVo.setCommentTime(comments.getCreateTime());
            if (zanMapper.queryIdByUserIdAndZanObjId(uid, String.valueOf(comments.getId()))!= null){
                commentPageVo.setLikeStatus(true);
            }
            commentPageVo.setAtType(comments.getAtType());
            if (comments.getAtType() == 1){
                CommentPageVo.AtContent atContent = new CommentPageVo.AtContent();
                atContent.setAtUid(comments.getAtUserId());
                atContent.setAtNickName(userMapper.queryNickNameByUid(comments.getAtUserId()));
                commentPageVo.setAtContent(atContent);
            }
            if (comments.getReplyContent() != null){
                CommentPageVo.AuthorReply authorReply = new CommentPageVo.AuthorReply();
                authorReply.setReplyContent(comments.getReplyContent());
                authorReply.setReplyTime(comments.getReplyTime());
                authorReply.setAuthorNickName(authorNickName);
                authorReply.setAuthorAvatar(authorAvatar);
                commentPageVo.setReplyState(true);
                commentPageVo.setAuthorReply(authorReply);
            }
            commentPageVo.setNickName(userMapper.queryNickNameByUid(comments.getUserId()));
            commentPageVo.setAvatar(userMapper.queryAvatarByUid(comments.getUserId()));

            // 添加设置封面
            commentPageVo.setCoverPath(videoMapper.queryCoverPathById(comments.getVideoId()));
            // 添加设置视频链接
            commentPageVo.setVideoUrl(videoMapper.queryVideoUrlById(comments.getVideoId()));
            commentPageVoArrayList.add(commentPageVo);
        }
        return commentPageVoArrayList;
    }

    @Override
    public IPage<Comments> queryCommentsPage(int page, int pageSize) {
        Page<Comments> p = new Page<>(page, pageSize);
        return commentsMapper.queryCommentsPage(p);
    }
}
